package com.ailk.tcm.hffw.android.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

public class DialogUtil {
	
	private Context context;
	
	public DialogUtil(Context context){
		this.context = context;
	}
	
	/**
	 * 弹出提示框
	 * @author chris
	 * @param message
	 * @since 2013-1-14 下午11:59:35
	 */
	public void promptInfoDialog(String message){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示");
		builder.setMessage(message);
		builder.setNegativeButton("知道了", null);
		builder.show();
	}
	
	/**
	 * 弹出包含一个按钮的确认框
	 * @author chris
	 * @param title
	 * @param message
	 * @param positiveAction
	 * @since 2013-1-14 下午11:59:43
	 */
	public void confirmDialog(String title, 
			String message, OnClickListener positiveAction){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("确定", positiveAction);
		builder.show();
	}
	
	/**
	 * 弹出包含两个按钮的确认框
	 * @author chris
	 * @param title
	 * @param message
	 * @param positiveAction
	 * @param negativeAction
	 * @since 2013-1-15 上午12:00:28
	 */
	public void confirmDialog(String title, String message, 
			OnClickListener positiveAction, OnClickListener negativeAction){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("确定", positiveAction);
		builder.setNegativeButton("取消", negativeAction);
		builder.show();
	}
	
	/**
	 * 弹出包含指定view的确认框
	 * @author chris
	 * @param ctx
	 * @param view
	 * @since 2013-2-17 下午4:21:52
	 */
	public void viewDialog(Context ctx ,View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx)
			.setView(view);
		builder.setPositiveButton("确定", null);
		builder.create().show();
	}
	
	/**
	 * 创建已有布局文件的窗口
	 * @author chris
	 * @param layout	窗口布局文件
	 * @param width		窗口宽度
	 * @param height	窗口高度
	 * @param viewInitializer	初始化窗口布局
	 * @return
	 */
	public PopupWindow popupWindow(int layout, int width, int height, ViewInitializer viewInitializer){
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(layout, null);
        PopupWindow popupWindow = new PopupWindow(view, width, height);
        //填充布局内容
        viewInitializer.initialize(view);
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        
        return popupWindow;
	}
	
	/**
	 * 创建已有布局文件的窗口,自动宽高
	 * @author chris
	 * @param layout	窗口布局文件
	 * @param viewInitializer	初始化窗口布局
	 * @return
	 */
	public PopupWindow popupWindow(int layout, ViewInitializer viewInitializer){        
        return popupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, viewInitializer);
	}
	
	/**
	 * 初始化窗口布局辅助接口
	 * @author chris
	 */
	public static interface ViewInitializer{
		/**
		 * 初始化窗口布局
		 * @author chris
		 * @param view	已经实例化的初始布局
		 */
		public void initialize(View view);
	}
}
